from flask import Blueprint
from flask import render_template, flash, request

from tzq.extensions import db
from tzq.tpc_db_info.tpc_db_info_models import UserLogin, DatabaseInfo, DatabasePermission

# 创建蓝图实例，设置前缀为'/tpc_db_info'
tpc_db_info_bp = Blueprint(
    'tpc_db_info',
    __name__,
    url_prefix='/tpc_db_info',
    static_folder='static',  # 模块专用静态资源
    template_folder='templates'  # 指定蓝图专属模板目录
)


# 导入路由（避免循环导入）


@tpc_db_info_bp.route('/', methods=['GET', 'POST'])
def tpc_db_info():
    client_ip = request.remote_addr
    flash(f'Your IP address is: {client_ip}  (无权限的联系twx1014920)')
    print(f'Your IP address is: {client_ip}')

    # 记录登录日志
    user_login = UserLogin(ip_address=client_ip)
    db.session.add(user_login)
    db.session.commit()
    # flash('You login.')

    database_info = DatabaseInfo.query.filter(
        db.exists().where(
            DatabaseInfo.database_code == DatabasePermission.database_code,
            DatabasePermission.ip_address == client_ip,
        )
    ).all()
    # database_info = DatabaseInfo.query.all()
    # return render_template('tpc_db_info/tpc_db_info.html', database_info=database_info)
    return render_template('tpc_db_info.html', database_info=database_info)

# from ..tzq import db
# from ..__main__ import db
